<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>在ubuntu上的18个非常实用的命令行工具软件 - whowin - 发表我个人原创作品的技术博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="whowin" /><meta name="description" content="使用Ubuntu的过程中，在终端上使用命令行工具是非常常见的事情，熟练地掌握命令行工具是使用ubuntu必不可少的技能，即便是Ubuntu的初学者，通常也很熟悉诸如ls、rm、cp等一些文件操作工具，当浏览/bin目录时，你会发现Ubuntu还有许多工具软件，本文将向读者简单介绍18个在Ubuntu上使用的命令行工具软件，本文不会详细介绍每个命令的用法，有对某个命令感兴趣的读者可以自行查找更详细的资料或者使用man在线手册，本文非常适合初学者阅读。
" /><meta name="keywords" content="linux, socket, hugo, dos" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://whowin.gitee.io/post/blog/linux/0010-19-useful-tools-in-ubuntu/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.e3fea119b1980e848b03dffbeddb11dd0fba483eed0e5f11870fb8e31f145bbd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="在ubuntu上的18个非常实用的命令行工具软件" />
<meta property="og:description" content="使用Ubuntu的过程中，在终端上使用命令行工具是非常常见的事情，熟练地掌握命令行工具是使用ubuntu必不可少的技能，即便是Ubuntu的初学者，通常也很熟悉诸如ls、rm、cp等一些文件操作工具，当浏览/bin目录时，你会发现Ubuntu还有许多工具软件，本文将向读者简单介绍18个在Ubuntu上使用的命令行工具软件，本文不会详细介绍每个命令的用法，有对某个命令感兴趣的读者可以自行查找更详细的资料或者使用man在线手册，本文非常适合初学者阅读。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://whowin.gitee.io/post/blog/linux/0010-19-useful-tools-in-ubuntu/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2024-01-11T16:43:29+08:00" />
<meta property="article:modified_time" content="2024-01-11T16:43:29+08:00" />

<meta itemprop="name" content="在ubuntu上的18个非常实用的命令行工具软件">
<meta itemprop="description" content="使用Ubuntu的过程中，在终端上使用命令行工具是非常常见的事情，熟练地掌握命令行工具是使用ubuntu必不可少的技能，即便是Ubuntu的初学者，通常也很熟悉诸如ls、rm、cp等一些文件操作工具，当浏览/bin目录时，你会发现Ubuntu还有许多工具软件，本文将向读者简单介绍18个在Ubuntu上使用的命令行工具软件，本文不会详细介绍每个命令的用法，有对某个命令感兴趣的读者可以自行查找更详细的资料或者使用man在线手册，本文非常适合初学者阅读。"><meta itemprop="datePublished" content="2024-01-11T16:43:29+08:00" />
<meta itemprop="dateModified" content="2024-01-11T16:43:29+08:00" />
<meta itemprop="wordCount" content="2726">
<meta itemprop="keywords" content="Linux,ubuntu," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="在ubuntu上的18个非常实用的命令行工具软件"/>
<meta name="twitter:description" content="使用Ubuntu的过程中，在终端上使用命令行工具是非常常见的事情，熟练地掌握命令行工具是使用ubuntu必不可少的技能，即便是Ubuntu的初学者，通常也很熟悉诸如ls、rm、cp等一些文件操作工具，当浏览/bin目录时，你会发现Ubuntu还有许多工具软件，本文将向读者简单介绍18个在Ubuntu上使用的命令行工具软件，本文不会详细介绍每个命令的用法，有对某个命令感兴趣的读者可以自行查找更详细的资料或者使用man在线手册，本文非常适合初学者阅读。"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

  <script async src="/js/busuanzi.pure.mini.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9724909319263152"
     crossorigin="anonymous"></script>


</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">WhoWin</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章归档</li>
      </a><a href="/article-categories/categories/">
        <li class="mobile-menu-item">文章分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">文章标签</li>
      </a><a href="/about/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">WhoWin</a>
  
  <div style="position:absolute; left: 80px; top: 75px; color: crimson">
      ———开源和分享是技术发展的源泉和动力；本博客所有文章均为原创
  </div>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/article-categories/categories/">文章分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">文章标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">在ubuntu上的18个非常实用的命令行工具软件</h1>

      <div class="post-meta">
        <span class="post-time"> 2024-01-11 </span>
        <div class="post-category">
            <a href="/categories/linux/"> Linux </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-find---在文件系统中查找所需的文件">1 <code>find</code> - 在文件系统中查找所需的文件</a></li>
        <li><a href="#2-grep---在文件中查找字符串">2 <code>grep</code> - 在文件中查找字符串</a></li>
        <li><a href="#3-cut---打印文件中指定的字段列">3 <code>cut</code> - 打印文件中指定的字段(列)</a></li>
        <li><a href="#4-bc---命令行上的计算器">4 <code>bc</code> - 命令行上的计算器</a></li>
        <li><a href="#5-comm---比较两个已排序的文件">5 <code>comm</code> - 比较两个已排序的文件</a></li>
        <li><a href="#6-diff---逐行比较两个文件">6 <code>diff</code> - 逐行比较两个文件</a></li>
        <li><a href="#7-df---文件系统的磁盘空间使用情况">7 <code>df</code> - 文件系统的磁盘空间使用情况</a></li>
        <li><a href="#8-du---估算文件系统的磁盘使用情况">8 <code>du</code> - 估算文件系统的磁盘使用情况</a></li>
        <li><a href="#9-file---查看文件类型">9 <code>file</code> - 查看文件类型</a></li>
        <li><a href="#10-fold---打印文件内容时在指定宽度位置添加换行">10 <code>fold</code> - 打印文件内容时，在指定宽度位置添加&quot;换行&quot;</a></li>
        <li><a href="#11-headtail---显示文件的第一行最后一行">11 <code>head/tail</code> - 显示文件的第一行/最后一行</a></li>
        <li><a href="#12-join---将两个具有公共字段的文件合并在一起">12 <code>join</code> - 将两个具有公共字段的文件合并在一起</a></li>
        <li><a href="#13-od---以各种不同的格式显示文件">13 <code>od</code> - 以各种不同的格式显示文件</a></li>
        <li><a href="#14-paste---合并文件行">14 <code>paste</code> - 合并文件行</a></li>
        <li><a href="#15-sort---对文件进行排序">15 <code>sort</code> - 对文件进行排序</a></li>
        <li><a href="#16-uniq---从一个已排序的文件中删除重复的行">16 <code>uniq</code> - 从一个已排序的文件中删除重复的行</a></li>
        <li><a href="#17-split---将文件分割为多个部分">17 <code>split</code> - 将文件分割为多个部分</a></li>
        <li><a href="#18-wc---打印文件中的字符数单词数和行数">18 <code>wc</code> - 打印文件中的字符数、单词数和行数</a></li>
        <li><a href="#欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></a></li>
        <li><a href="#欢迎订阅-进程间通信专栏httpsblogcsdnnetwhowincategory_12404164html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12404164.html">『进程间通信专栏』</a></strong></a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>使用Ubuntu的过程中，在终端上使用命令行工具是非常常见的事情，熟练地掌握命令行工具是使用ubuntu必不可少的技能，即便是Ubuntu的初学者，通常也很熟悉诸如<code>ls</code>、<code>rm</code>、<code>cp</code>等一些文件操作工具，当浏览<code>/bin</code>目录时，你会发现Ubuntu还有许多工具软件，本文将向读者简单介绍18个在Ubuntu上使用的命令行工具软件，本文不会详细介绍每个命令的用法，有对某个命令感兴趣的读者可以自行查找更详细的资料或者使用man在线手册，本文非常适合初学者阅读。</p>
<hr>
<h2 id="1-find---在文件系统中查找所需的文件">1 <code>find</code> - 在文件系统中查找所需的文件</h2>
<ul>
<li>在当前目录及其子目录下查找 C 语言的源程序：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ find . -name *.c
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>在 <code>/var</code> 目录及其子目录下查找文件大小大于 <code>100kb</code> 的日志文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ sudo find /var -name &#34;*.log&#34; -size +100k
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>在当前目录及其子目录下查找大于 2M 的临时文件，如果其所属用户不是 <code>developer</code>，则将其删除：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ find . -name &#34;*tmp&#34; -size +2M ! -user developer -exec rm {} \;
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>要求在删除文件前确认：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ find . -name &#34;*tmp&#34; -size +2M ! -user developer -ok rm {} \;
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="2-grep---在文件中查找字符串">2 <code>grep</code> - 在文件中查找字符串</h2>
<ul>
<li>在当前目录下的文本文件中，查找含有 &ldquo;hello&rdquo; 字符串的文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ grep &#34;hello&#34; *.txt
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>在当前目录及其子目录下的所有文件中，查找含有 &ldquo;hello&rdquo; 字符串的文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ grep -r &#34;hello&#34; ./
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>在当前目录及其子目录下的 C 语言源代码文件中，查找含有 &ldquo;hello&rdquo; 字符串的文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ grep -r &#34;hello&#34; ./ --include=&#34;*.c&#34;
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>使用正则表达式查找文件中的字符串：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ grep -r &#34;uid-[0-9]*&#34; ./ --include=&#34;*.c&#34;
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="3-cut---打印文件中指定的字段列">3 <code>cut</code> - 打印文件中指定的字段(列)</h2>
<ul>
<li>打印文件 <code>/etc/fstab</code> 文件中每行的前 12 个字符
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cut -c1-12 /etc/fstab
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>打印文件 <code>/etc/passwd</code> 中第 1、6、7 字段的内容，字段分隔符为 &ldquo;:&rdquo;
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cut -d: -f1,6,7 /etc/passwd
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="4-bc---命令行上的计算器">4 <code>bc</code> - 命令行上的计算器</h2>
<ul>
<li>这是一个命令行下的交互式计算器，可以使用变量，还可以使用数学函数，<code>quit</code> 命令退出交互；
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ bc -l
</span></span><span class="line"><span class="cl">bc 1.07.1
</span></span><span class="line"><span class="cl">Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
</span></span><span class="line"><span class="cl">This is free software with ABSOLUTELY NO WARRANTY.
</span></span><span class="line"><span class="cl">For details type `warranty&#39;. 
</span></span><span class="line"><span class="cl">102+240*3.5
</span></span><span class="line"><span class="cl">942.0
</span></span><span class="line"><span class="cl">a=102
</span></span><span class="line"><span class="cl">b=240
</span></span><span class="line"><span class="cl">c=3.5
</span></span><span class="line"><span class="cl">a+b*c
</span></span><span class="line"><span class="cl">942.0
</span></span><span class="line"><span class="cl">b/c
</span></span><span class="line"><span class="cl">68.57142857142857142857
</span></span><span class="line"><span class="cl">sqrt(a)
</span></span><span class="line"><span class="cl">10.09950493836207795336
</span></span><span class="line"><span class="cl">quit
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>也可以在脚本中使用：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ x=100
</span></span><span class="line"><span class="cl">$ y=20
</span></span><span class="line"><span class="cl">$ echo &#34;$x/$y&#34; | bc -l
</span></span><span class="line"><span class="cl">5.00000000000000000000
</span></span><span class="line"><span class="cl">$ echo &#34;$x/$y&#34; | bc
</span></span><span class="line"><span class="cl">5
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="5-comm---比较两个已排序的文件">5 <code>comm</code> - 比较两个已排序的文件</h2>
<ul>
<li>为了演示这个命令，先制作两个简单的文本文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ echo -e &#34;avi\ndani\nrina\nzina&#34;&gt;student1
</span></span><span class="line"><span class="cl">$ echo -e &#34;avi\ndina\nmeni\nzina&#34;&gt;student2
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>用 comm 比较这两个文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat student1
</span></span><span class="line"><span class="cl">avi
</span></span><span class="line"><span class="cl">dani
</span></span><span class="line"><span class="cl">rina
</span></span><span class="line"><span class="cl">zina
</span></span><span class="line"><span class="cl">$ cat student2
</span></span><span class="line"><span class="cl">avi
</span></span><span class="line"><span class="cl">dina
</span></span><span class="line"><span class="cl">meni
</span></span><span class="line"><span class="cl">zina
</span></span><span class="line"><span class="cl">$ comm student1 student2
</span></span><span class="line"><span class="cl">                avi
</span></span><span class="line"><span class="cl">dani
</span></span><span class="line"><span class="cl">        dina
</span></span><span class="line"><span class="cl">        meni
</span></span><span class="line"><span class="cl">rina
</span></span><span class="line"><span class="cl">                zina
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>第 1 列是第 1 个文件的内容，第 2 列是第 2 个文件内容，第 3 列是两个文件中相同的内容；</li>
</ul>
<hr>
<h2 id="6-diff---逐行比较两个文件">6 <code>diff</code> - 逐行比较两个文件</h2>
<ul>
<li>使用上面使用过的文件 student1 和 student2 进行比较：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ diff student1 student2
</span></span><span class="line"><span class="cl">2,3c2,3
</span></span><span class="line"><span class="cl">&lt; dani
</span></span><span class="line"><span class="cl">&lt; rina
</span></span><span class="line"><span class="cl">---
</span></span><span class="line"><span class="cl">&gt; dina
</span></span><span class="line"><span class="cl">&gt; meni
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>其中 <code>2,3c2,3</code> 的含义是：将第 1 个文件中的第 2、3 行改为第 2 个文件的第 2、3 行，两个文件就一样了；</li>
</ul>
<hr>
<h2 id="7-df---文件系统的磁盘空间使用情况">7 <code>df</code> - 文件系统的磁盘空间使用情况</h2>
<pre><code>```
$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              211908       0    211908   0% /dev
tmpfs              48496     712     47784   2% /run
/dev/vda1        6105676 4262704   1522648  74% /
tmpfs             242468       0    242468   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             242468       0    242468   0% /sys/fs/cgroup
tmpfs              48492       0     48492   0% /run/user/1000
$    
```
</code></pre>
<hr>
<h2 id="8-du---估算文件系统的磁盘使用情况">8 <code>du</code> - 估算文件系统的磁盘使用情况</h2>
<ul>
<li>使用这个命令通常都要加上参数 <code>-h</code>，这样显示的结果才比较清楚：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ du -h
</span></span><span class="line"><span class="cl">4.0K    ./.config/procps
</span></span><span class="line"><span class="cl">8.0K    ./.config
</span></span><span class="line"><span class="cl">28K     ./.mitmproxy
</span></span><span class="line"><span class="cl">4.0K    ./.cache
</span></span><span class="line"><span class="cl">22M     ./frp
</span></span><span class="line"><span class="cl">51M     .
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>可以不显示子目录，仅显示汇总结果：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ du -h -s
</span></span><span class="line"><span class="cl">51M     .
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>还可以指定子目录的深度，比如：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ du -h -d 1
</span></span><span class="line"><span class="cl">8.0K    ./.config
</span></span><span class="line"><span class="cl">28K	    ./.mitmproxy
</span></span><span class="line"><span class="cl">4.0K    ./.cache
</span></span><span class="line"><span class="cl">22M     ./frp
</span></span><span class="line"><span class="cl">51M     .
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>与前面的 <code>du -h</code> 相比，这次没有显示目录 <code>./.config/procps</code>，这是因为 <code>-d 1</code> 指定了目录深度只有 1 层。</li>
</ul>
<hr>
<h2 id="9-file---查看文件类型">9 <code>file</code> - 查看文件类型</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ file mytest.c
</span></span><span class="line"><span class="cl">mytest.c: C source, ASCII text, with CRLF line terminators
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ file /bin/cp
</span></span><span class="line"><span class="cl">/bin/cp: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=421e1abd8faf1cb290df755a558377c5d7def3b1, for GNU/Linux 3.2.0, stripped
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ file /lib64/ld-linux-x86-64.so.2
</span></span><span class="line"><span class="cl">/lib64/ld-linux-x86-64.so.2: symbolic link to /lib/x86_64-linux-gnu/ld-2.31.so
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div><hr>
<h2 id="10-fold---打印文件内容时在指定宽度位置添加换行">10 <code>fold</code> - 打印文件内容时，在指定宽度位置添加&quot;换行&quot;</h2>
<ul>
<li>为演示 <code>fold</code> 命令，先制作一个文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ echo &#34;id-2093384 id-8984773 id-8725536 id-9828835 id-6455351 id-9873773 &#34;&gt;data1
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>执行下面命令看一下打印效果：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat data1
</span></span><span class="line"><span class="cl">id-2093384 id-8984773 id-8725536 id-9828835 id-6455351 id-9873773 
</span></span><span class="line"><span class="cl">$ fold -w 11 data1
</span></span><span class="line"><span class="cl">id-2093384 
</span></span><span class="line"><span class="cl">id-8984773 
</span></span><span class="line"><span class="cl">id-8725536 
</span></span><span class="line"><span class="cl">id-9828835 
</span></span><span class="line"><span class="cl">id-6455351 
</span></span><span class="line"><span class="cl">id-9873773 
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="11-headtail---显示文件的第一行最后一行">11 <code>head/tail</code> - 显示文件的第一行/最后一行</h2>
<ul>
<li>显示 <code>/etc/passwd</code> 文件的最前面的 2 行：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ head -2 /etc/passwd
</span></span><span class="line"><span class="cl">root❌0:0:root:/root:/bin/bash
</span></span><span class="line"><span class="cl">daemon❌1:1:daemon:/usr/sbin:/usr/sbin/nologin
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>显示 <code>/etc/passwd</code> 文件的最后 2 行：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ tail -2 /etc/passwd
</span></span><span class="line"><span class="cl">snap_daemon❌584788:584788::/nonexistent:/usr/bin/false
</span></span><span class="line"><span class="cl">glances❌128:135::/var/lib/glances:/usr/sbin/nologin
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>显示 <code>/etc/passwd</code> 文件的前 50 个字符：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ head -c 50 /etc/passwd
</span></span><span class="line"><span class="cl">root❌0:0:root:/root:/bin/bash
</span></span><span class="line"><span class="cl">daemon❌1:1:daemo$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="12-join---将两个具有公共字段的文件合并在一起">12 <code>join</code> - 将两个具有公共字段的文件合并在一起</h2>
<ul>
<li>为了演示 <code>join</code> 命令，先制作 2 个文件：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ echo -e &#34;avi haifa\ndani aco\nrina tel aviv\nzina ny&#34;&gt;s1
</span></span><span class="line"><span class="cl">$ echo -e &#34;avi 1002\ndani 2000\nrina 3000\nzina 4255&#34;&gt;s2
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>显示这两个文件
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat s1
</span></span><span class="line"><span class="cl">avi haifa
</span></span><span class="line"><span class="cl">dani aco
</span></span><span class="line"><span class="cl">rina tel aviv
</span></span><span class="line"><span class="cl">zina ny
</span></span><span class="line"><span class="cl">$ cat s2
</span></span><span class="line"><span class="cl">avi 1002
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">zina 4255
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>可以看到这两个文件的第一列是一样的，这就是这两个文件的公共字段，这样的文件就可以使用 <code>join</code> 合并：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ join -j1 1 -j2 1 s1 s2
</span></span><span class="line"><span class="cl">avi haifa 1002
</span></span><span class="line"><span class="cl">dani aco 2000
</span></span><span class="line"><span class="cl">rina tel aviv 3000
</span></span><span class="line"><span class="cl">zina ny 4255
</span></span><span class="line"><span class="cl">$ 
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>其中 <code>-j1 1</code> 表示第一个文件的第一个字段，<code>-j2 1</code> 表示第二个文件的第一个字段。</li>
</ul>
<hr>
<h2 id="13-od---以各种不同的格式显示文件">13 <code>od</code> - 以各种不同的格式显示文件</h2>
<ul>
<li>先制作一个文件
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ echo -e &#34;hello&#34;&gt;a1
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>正常显示文件
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat a1
</span></span><span class="line"><span class="cl">hello
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>使用 <code>od</code> 按八进制和字符显示文件
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ od -bc a1
</span></span><span class="line"><span class="cl">0000000 150 145 154 154 157 012
</span></span><span class="line"><span class="cl">          h   e   l   l   o  \n
</span></span><span class="line"><span class="cl">0000006
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>按 16 进制字和字符显示文件
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ od -xc a1
</span></span><span class="line"><span class="cl">0000000    6568    6c6c    0a6f
</span></span><span class="line"><span class="cl">          h   e   l   l   o  \n
</span></span><span class="line"><span class="cl">0000006
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>按 16 进制字节和字符显示文件
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ od -t x1c a1
</span></span><span class="line"><span class="cl">0000000  68  65  6c  6c  6f  0a
</span></span><span class="line"><span class="cl">          h   e   l   l   o  \n
</span></span><span class="line"><span class="cl">0000006
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="14-paste---合并文件行">14 <code>paste</code> - 合并文件行</h2>
<ul>
<li>制作一个新文件 s3
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ echo -e &#34;20003 france\n09388 uk\n20019 italy\n98377 spain&#34;&gt;s3
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>使用 <code>paste</code> 命令将前面用过的文件 s2 和 s3 文件合并
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat s2
</span></span><span class="line"><span class="cl">avi 1002
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">zina 4255
</span></span><span class="line"><span class="cl">$ cat s3
</span></span><span class="line"><span class="cl">20003 france
</span></span><span class="line"><span class="cl">09388 uk
</span></span><span class="line"><span class="cl">20019 italy
</span></span><span class="line"><span class="cl">98377 spain
</span></span><span class="line"><span class="cl">$ paste s2 s3
</span></span><span class="line"><span class="cl">avi 1002	20003 france
</span></span><span class="line"><span class="cl">dani 2000	09388 uk
</span></span><span class="line"><span class="cl">rina 3000	20019 italy
</span></span><span class="line"><span class="cl">zina 4255	98377 spain
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="15-sort---对文件进行排序">15 <code>sort</code> - 对文件进行排序</h2>
<ul>
<li>使用 s3 文件演示 <code>sort</code> 的作用：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat s3
</span></span><span class="line"><span class="cl">20003 france
</span></span><span class="line"><span class="cl">09388 uk
</span></span><span class="line"><span class="cl">20019 italy
</span></span><span class="line"><span class="cl">98377 spain
</span></span><span class="line"><span class="cl">$ sort s3
</span></span><span class="line"><span class="cl">09388 uk
</span></span><span class="line"><span class="cl">20003 france
</span></span><span class="line"><span class="cl">20019 italy
</span></span><span class="line"><span class="cl">98377 spain
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="16-uniq---从一个已排序的文件中删除重复的行">16 <code>uniq</code> - 从一个已排序的文件中删除重复的行</h2>
<ul>
<li>以前面用过的 s2 文件演示 <code>uniq</code> 命令
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ echo -e &#34;dani 2000\nrina 3000&#34;&gt;&gt;s2
</span></span><span class="line"><span class="cl">$ cat s2
</span></span><span class="line"><span class="cl">avi 1002
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">zina 4255
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">$ sort s2&gt;s4
</span></span><span class="line"><span class="cl">$ cat s4
</span></span><span class="line"><span class="cl">avi 1002
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">zina 4255
</span></span><span class="line"><span class="cl">$ uniq s4
</span></span><span class="line"><span class="cl">avi 1002
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">zina 4255
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
<hr>
<h2 id="17-split---将文件分割为多个部分">17 <code>split</code> - 将文件分割为多个部分</h2>
<ul>
<li>以 s2 文件为例，下面 <code>split</code> 命令的意思是：将 s2 文件按照 2 行一个文件分割为若干个文件，新文件名称以 <code>gen_</code> 开头
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cat s2
</span></span><span class="line"><span class="cl">avi 1002
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">zina 4255
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">$ split -2 s2 gen_
</span></span><span class="line"><span class="cl">$ cat gen_aa
</span></span><span class="line"><span class="cl">avi 1002
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">$ cat gen_ab
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">zina 4255
</span></span><span class="line"><span class="cl">$ cat gen_ac
</span></span><span class="line"><span class="cl">dani 2000
</span></span><span class="line"><span class="cl">rina 3000
</span></span><span class="line"><span class="cl">$
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>因为 s2 文件有 6 行，所以被分割成了 3 个文件，文件名分别是：<code>gen_aa</code>、<code>gen_ab</code> 和 <code>gen_ac</code></li>
</ul>
<hr>
<h2 id="18-wc---打印文件中的字符数单词数和行数">18 <code>wc</code> - 打印文件中的字符数、单词数和行数</h2>
<ul>
<li>默认情况下，打印出文件的行数、单词数和字符数：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ wc /etc/passwd
</span></span><span class="line"><span class="cl">  51   90 3070 /etc/passwd
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>其中：<code>51</code> 是文件行数，<code>90</code> 是文件中的单词数，<code>3070</code> 是该文件的字符数；</li>
<li>可以使用参数 <code>-l</code> 表示行数，<code>-w</code> 表示单词数，<code>-c</code> 表示字符数；
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ wc -l /etc/passwd
</span></span><span class="line"><span class="cl">51 /etc/passwd
</span></span><span class="line"><span class="cl">$ wc -c /etc/passwd
</span></span><span class="line"><span class="cl">3070 /etc/passwd
</span></span><span class="line"><span class="cl">$ wc -w /etc/passwd
</span></span><span class="line"><span class="cl">90 /etc/passwd
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>还可以这样用：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ ps aux | wc -l
</span></span><span class="line"><span class="cl">381
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>表示 <code>ps aux</code> 这个命令的输出有 381 行。</li>
</ul>
<h2 id="欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></h2>
<h2 id="欢迎订阅-进程间通信专栏httpsblogcsdnnetwhowincategory_12404164html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12404164.html">『进程间通信专栏』</a></strong></h2>
<hr>
<p><strong>欢迎访问我的博客：https://whowin.cn</strong></p>
<p><strong>email: <a href="mailto:hengch@163.com">hengch@163.com</a></strong></p>
<p><img src="https://whowin.gitee.io/images/qrcode/sponsor-qrcode.png" alt="donation"></p>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">whowin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2024-01-11
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/linux/">Linux</a>
          <a href="/tags/ubuntu/">ubuntu</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/blog/network/0023-using-signals-with-blocking-socket-apis/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">使用signal中止阻塞的socket函数的应用实例</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/blog/linux/0027-how-to-check-ram-usage-in-ubuntu/">
            <span class="next-text nav-default">在ubuntu上检查内存使用情况的九种方法</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/blog/linux/0010-19-useful-tools-in-ubuntu/" class="leancloud_visitors" data-flag-title="在ubuntu上的18个非常实用的命令行工具软件">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'OFCGzCfJRUglzOdzrqMGkbTR-gzGzoHsz',
        appKey: 'v7P29kPAEbsmaavaYPNhGhnF',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:hengch@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://whowin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>
<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2022 - 
    2024<span class="heart"><i class="iconfont icon-heart"></i></span><span>whowin</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
